import 'package:flutter/material.dart';
import 'package:flutter_flux/flutter_flux.dart';
import 'package:freemusic/components/InkWellContainer.dart';
import 'package:freemusic/components/Player.dart';
import 'package:freemusic/components/SongItem.dart';
import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/LocalSong.dart';
import 'package:freemusic/services/SongServices.dart';
import 'package:freemusic/stores/PlayerStores.dart';
import 'package:freemusic/stores/SongStores.dart';
import 'package:freemusic/utils/Util.dart';

class LocalSongPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LocalSongPageState();
  }
}

class LocalSongPageState extends State<LocalSongPage> with StoreWatcherMixin<LocalSongPage> {

  SongStore _songStore;
  PlayerStore _playerStore;

  @override
  void initState() {
    super.initState();
    _songStore = listenToStore(songStoreToken);
    _playerStore = listenToStore(playerStoreToken);
    SongServices.getInstace().loadLocalSongs();
  }
  @override
  Widget build(BuildContext context) {
    Color themeColor = Util.themeColor;
    List<LocalSong> songs = _songStore.localSongs;
    AbstractSong playingSong = _playerStore.current;
    return Scaffold(
      appBar: AppBar(
        title: Text("本地音乐"),
        centerTitle: true,
        backgroundColor: themeColor,
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            InkWellContainer(
              onTap: (){
                SongServices.getInstace().playListAllSong(songs);
              },
              image: Icon(
                Icons.play_circle_filled,
                color: themeColor,
                size: 32,
              ),
              text: "播放全部 ( ${songs.length} 首)",
            ),
            Expanded(
              child: ListView.builder(
                itemCount: songs.length,
                itemBuilder: (context, index) {
                  return SongItem(song: songs[index], index: index +1 , type: SongItemType.Local, playingHash: playingSong.hash,);
                },
              ),
            ),
            PlayerWidget(),
          ],
        ),
      ),
    );
  }
}
